package cn.ilikes.tools.nosql.jpa.query.iquerys;

import java.util.HashMap;
import java.util.Properties;

import java.util.Map;
import javax.persistence.Parameter;

import cn.ilikes.tools.nosql.jpa.clients.AbClient;
import cn.ilikes.tools.nosql.jpa.clients.indexs.IClientIndex;
import cn.ilikes.tools.nosql.jpa.persistences.contexts.AbstractContext;
import cn.ilikes.tools.nosql.jpa.query.HQueryImpl.SQLQueryType;
import cn.ilikes.tools.nosql.jpa.query.sql.element.SQLTable;

public abstract class JPAQuery extends JPAQueryImpl implements IQuery {

	protected Map<Parameter<?>, Object> parameters = new HashMap<Parameter<?>, Object>();

	protected SQLTable jPATable;

	protected SqlType sqlType;

	protected String sql;

	protected QueryType queryType = QueryType.List;

	protected SQLQueryType sqlQueryType = SQLQueryType.General;

	protected Properties queryProperties;

	protected int firstResult = IClientIndex.INVALID;

	protected int maxResults = IClientIndex.INVALID;

	public JPAQuery(AbClient client, AbstractContext context, String sql) {
		super(client, context);
		this.sql = sql;

	}

	public SqlType getSqlType() {
		return sqlType;
	}

	public void setSqlType(SqlType sqlType) {
		this.sqlType = sqlType;
	}

	public SQLTable getjPATable() {
		return jPATable;
	}

	public void setjPATable(SQLTable jPATable) {
		this.jPATable = jPATable;
	}

	@Override
	public String getQuerySql() {
		return sql;
	}

	@Override
	public boolean isDelete() {
		return sqlType.equals(SqlType.delete);
	}

	@Override
	public boolean isSelect() {
		return sqlType.equals(SqlType.select);
	}

	@Override
	public boolean isUpdate() {
		return sqlType.equals(SqlType.update);
	}

	public void setQueryType(QueryType queryType) {
		this.queryType = queryType;
	}

	public QueryType getQueryType() {
		return queryType;
	}

	@Override
	public <T> void setParameter(Parameter<T> parameter, Object value) {
		parameters.put(parameter, value);
	}

	@Override
	public void init() {

	}

	public int getFirstResult() {
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public abstract Object queryResult();

	public SQLQueryType getSqlQueryType() {
		return sqlQueryType;
	}

	public void setSqlQueryType(SQLQueryType sqlQueryType) {
		this.sqlQueryType = sqlQueryType;
	}

	public Properties getQueryProperties() {
		return queryProperties;
	}

	public void setQueryProperties(Properties queryProperties) {
		this.queryProperties = queryProperties;
	}

}
